//
//  ZBImageTool.m
//  ZBMediaLib
//
//  Created by xzb on 2018/6/24.
//

#import "ZBImageTool.h"

@implementation ZBImageTool

+ (UIImage *)bdUploadImage:(UIImage *)image reqSize:(CGSize)reqSize compression:(CGFloat)compression
{
    NSLog(@"💧******* info *******💧\nimage:%@\nreqSize:%@\ncompression:%.2f", image, NSStringFromCGSize(reqSize), compression);
    NSUInteger calculateInSample = 0.0;
    UIImage *scaleImage, *resultImage;
    
    //1.计算压缩比例
    if (reqSize.width > 0 || reqSize.height > 0) {
        calculateInSample = [self inSampleSizeWithImage:image reqSize:reqSize];
        NSLog(@"💧******* calculateInSample *******💧\n%ld", calculateInSample);
    }
    
    //2.缩放图片
    if (calculateInSample > 0) {
        scaleImage = [self imageRawSizeWithImage:image maxLength:MAX(image.size.width, image.size.height) / calculateInSample];
        NSLog(@"💧******* scaleImage *******💧\n%@", scaleImage);
    }
    
    //3.质压
    if (scaleImage && compression > 0) {
        NSData *data = UIImageJPEGRepresentation(scaleImage, compression);
        resultImage = [UIImage imageWithData:data];
        NSLog(@"💧******* compressionImage *******💧\nresultImage:%@\nlength:%ld", resultImage, data.length / 1024);
    } else {
        resultImage = scaleImage;
    }
    return resultImage;
}

+ (NSUInteger)inSampleSizeWithImage:(UIImage *)image reqSize:(CGSize)reqSize
{
    CGFloat imgW = image.size.width;
    CGFloat imgH = image.size.height;
    NSUInteger inSampleSize = 1;
    if (imgH > reqSize.height || imgW > reqSize.width) {
        CGFloat halfH = imgH / 2;
        CGFloat halfW = imgW / 2;
        while ((halfH / inSampleSize) >= reqSize.height && (halfW / inSampleSize) >= reqSize.width) {
            inSampleSize *= 2;
        }
    }
    return inSampleSize;
}

+ (UIImage *)imageRawSizeWithImage:(UIImage *)image maxLength:(CGFloat)maxLength
{
    if (maxLength <= 0) {
        return nil;
    }
    NSInteger oldWidth = CGImageGetWidth(image.CGImage);
    NSInteger oldHeight = CGImageGetHeight(image.CGImage);
    int newWidth = 0;
    int newHeight = 0;
    if (oldHeight > oldWidth) {
        float rate = oldHeight / maxLength;
        newWidth = oldWidth / rate;
        newHeight = maxLength;
    } else {
        float rate = oldWidth / maxLength;
        newHeight = oldHeight / rate;
        newWidth = maxLength;
    }
    
    UIImage *scaleImage = [self imageByScalingAndCroppingForSize:CGSizeMake(newWidth, newHeight) image:image];
    return scaleImage;
}

+ (UIImage *)imageByScalingAndCroppingForSize:(CGSize)targetSize image:(UIImage *)image
{
    UIImage *sourceImage = image;
    UIImage *newImage = nil;
    CGSize imageSize = sourceImage.size;
    CGFloat width = imageSize.width;
    CGFloat height = imageSize.height;
    CGFloat targetWidth = targetSize.width;
    CGFloat targetHeight = targetSize.height;
    CGFloat scaleFactor = 0.0;
    CGFloat scaledWidth = targetWidth;
    CGFloat scaledHeight = targetHeight;
    CGPoint thumbnailPoint = CGPointMake(0.0, 0.0);
    if (CGSizeEqualToSize(imageSize, targetSize) == NO) {
        CGFloat widthFactor = targetWidth / width;
        CGFloat heightFactor = targetHeight / height;
        if (widthFactor > heightFactor)
            scaleFactor = widthFactor; // scale to fit height
        else
            scaleFactor = heightFactor; // scale to fit width
        scaledWidth = width * scaleFactor;
        scaledHeight = height * scaleFactor;
        // center the image
        if (widthFactor > heightFactor) {
            thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;
        } else if (widthFactor < heightFactor) {
            thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
        }
    }
    
    UIGraphicsBeginImageContextWithOptions(targetSize, YES, 1);
    CGRect thumbnailRect = CGRectZero;
    thumbnailRect.origin = thumbnailPoint;
    thumbnailRect.size.width = scaledWidth;
    thumbnailRect.size.height = scaledHeight;
    [sourceImage drawInRect:thumbnailRect];
    newImage = UIGraphicsGetImageFromCurrentImageContext();
    if (newImage == nil)
        UIGraphicsEndImageContext();
    return newImage;
}

+ (UIImage *)compressImageQuality:(UIImage *)image toByte:(NSInteger)maxLength
{
    CGFloat compression = 1;
    NSData *data = UIImageJPEGRepresentation(image, compression);
    while (data.length > maxLength && compression > 0) {
        compression -= 0.02;
        data = UIImageJPEGRepresentation(image, compression);
    }
    UIImage *resultImage = [UIImage imageWithData:data];
    return resultImage;
}


@end
